package model;

public class GameField {

	/**
	 * The next method createField returns array with all the Lines, Dots and
	 * Squares. Sets Lines and Squares references to surrounding squares and
	 * lines.
	 */

	public static FieldObject[][] createField(int inputRows, int inputCols) {

		/**
		 * In the next lines the array is created and populated with subclasses
		 * of FieldObject - Dot, Square, HorizontalLine and VerticalLine.
		 * Objects are created via default constructors. In the future versions
		 * they could be created with constructors with parameters, or default
		 * ones could be upgraded.
		 */

		FieldObject[][] field = new FieldObject[inputRows * 2 - 1][inputCols * 2 - 1];

		for (int row = 0; row < field.length; row++) {
			for (int col = 0; col < field[row].length; col++) {

				if (row % 2 == 0) {
					if (col % 2 == 0) {
						field[row][col] = new Dot();
					} else {
						field[row][col] = new LineHorizontal();
					}
				} else {
					if (col % 2 == 0) {
						field[row][col] = new LineVertical();
					} else {
						field[row][col] = new Square();
					}
				}
			}
		}

		/**
		 * For now the field is populated with default objects. No references in
		 * Lines and Squares are set. We are doing it in the next lines
		 */

		for (int row = 0; row < field.length; row++) {
			for (int col = 0; col < field[row].length; col++) {

				if (row % 2 == 0) {
					if (col % 2 != 0) {

						/**
						 * In the next code we are setting the references of
						 * surrounding squares in Line objects. The try-catch
						 * block is to prevent our program from crashing when
						 * trying to set surrounding Square in border lines. The
						 * catches will be empty which is bad practice, but this is
						 * the simplest way to prevent border Line bugs
						 */

						try {
							((LineHorizontal) field[row][col])
									.setUpperSquare((Square) field[row - 1][col]);
						} catch (ArrayIndexOutOfBoundsException e) {
							// e.printStackTrace();
							System.out.println("bam bam OTGORE");
						}

						try {
							((LineHorizontal) field[row][col])
									.setDownSquare((Square) field[row + 1][col]);
						} catch (Exception e) {
							// e.printStackTrace();
							System.out.println("bam bam OTDOLU");
						}
					}
				} else {
					if (col % 2 == 0) {

						try {
							((LineVertical) field[row][col])
									.setRightSquare((Square) field[row][col + 1]);
						} catch (Exception e) {
							// e.printStackTrace();
							System.out.println("bam bam OTDQSNO");
						}

						try {
							((LineVertical) field[row][col])
									.setLeftSquare((Square) field[row][col - 1]);
						} catch (Exception e) {
							// e.printStackTrace();
							System.out.println("bam bam OTLQVO");
						}

					} else {

						// here no catch-block is needed because we are sure we
						// have square surrounding objects

						((Square) field[row][col])
								.setSurroundingLineReferences(
										(Line) field[row][col - 1],
										(Line) field[row][col + 1],
										(Line) field[row - 1][col],
										(Line) field[row + 1][col]);
					}
				}
			}
		}

		return field;

	}
}
